package com.nmf.site.common;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class DoubleUtil {
	/**
	 * double 加法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static Double addDouble(Double a, Double b) {
		if (a == null) {
			a = 0.0d;
		}

		if (b == null) {
			b = 0.0d;
		}
		Double c = a + b;
		BigDecimal bc = new BigDecimal(c);
		Double f1 = bc.setScale(11, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	public static String getDoubleTOStr(Double d){
		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();   
		nf.setGroupingUsed(false);  
		return nf.format(d);
	}

	public static Double getDoubleByTwo(Double a) {
		if(a==null) {
			return null;
		}
		BigDecimal bc = new BigDecimal(a);
		Double f1 = bc.setScale(11, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
	
	public static Double getDoubleByTwo(Double a,int num) {
		if(a==null) {
			return null;
		}
		BigDecimal bc = new BigDecimal(a);
		Double f1 = bc.setScale(num, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	public static Double addDouble(Double a, Double b, Double c) {
		Double f1 = addDouble(addDouble(a, b), c);
		return f1;
	}
	
	public static Double addDouble(Double a, Double... bs) {
		Double ax = a;
		for(Double b : bs){
			ax = addDouble(ax,b);
		}
		return ax;
	}


	/**
	 * double 减法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static Double subtractionDouble(Double a, Double b) {
		if (a == null) {
			a = 0.0d;
		}

		if (b == null) {
			b = 0.0d;
		}
		Double c = a - b;
		BigDecimal bc = new BigDecimal(c);
		Double f1 = bc.setScale(11, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
	
	public static Double subtractionDouble(Double a, Double... bs) {
		Double ax = a;
		for(Double b : bs){
			ax = subtractionDouble(ax,b);
		}
		return ax;
	}

	public static Double multiplicationDouble(Double a, Double... bs) {
		Double ax = a;
		for(Double b : bs){
			ax = multiplicationDouble(ax,b);
		}
		return ax;
	}
	/**
	 * double 乘法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static Double multiplicationDouble(Double a, Double b) {
		if (a == null) {
			a = 0.0d;
		}

		if (b == null) {
			b = 0.0d;
		}
		
		BigDecimal bd1 = new BigDecimal(a);  
        BigDecimal bd2 = new BigDecimal(b); 
        
        BigDecimal rst = bd1.multiply(bd2);
		Double f1 = rst.setScale(11, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	/**
	 * double 除法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static Double divideDouble(Double a, Double b) {
		if (a == null) {
			a = 0.0d;
		}

		if (b == null) {
			b = 0.0d;
		}
		Double c = a / b;
		BigDecimal bc = new BigDecimal(c);
		Double f1 = bc.setScale(11, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
	
	public static Double divideDouble(Double a, Double... bs) {
		if (a == null) {
			a = 0.0d;
		}
		Double ax = a;
		for(Double b : bs){
			ax = divideDouble(ax,b);
		}
		return ax;
	}

	/**
	 * 计算百分比
	 * 
	 * @param a
	 *            分子
	 * @param b
	 *            分母，为0有异常
	 * @param decimal
	 *            设置精确位数
	 * @return
	 */
	public static String percent(Double a, Double b, Integer decimal) {
 
		if (a == null)
			a = 0.0;
		if (b == null)
			b = 1.0;
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(decimal);
		return numberFormat.format(a / b * 100) + "%";
	}

	/**
	 * 计算百分比
	 * 
	 * @param a
	 *            分子
	 * @param b
	 *            分母，为0有异常
	 * @param decimal
	 *            设置精确位数
	 * @return
	 */
	public static String percentReverseInteger(Double a, Double b) {
		if (a == null)
			a = 0.0;
		if (b == null)
			b = 1.0;
		if (to45(a / b * 100) < 0)
			return (to45(a / b * 100) + "%").replace("-", "+");
		else if(to45(a / b * 100) > 0)
			return "-" + to45(a / b * 100) + "%";
		else 
			return to45(a / b * 100) + "%"; 
	}

	/**
	 * 四舍五入运算
	 * 
	 * @param number
	 * @return
	 */
	public static Integer to45(Double number) {
		return number == null ? null : new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
	}
	
	public static Double getNoFloatFloor(Double number){
		return Math.floor(number);
	}
	
	/**
	 * 去掉小数位，向下取整
	 * @param number
	 * @return
	 */
	public static String getFloorStr(Double number){
		if(number == null){
			return "0";
		}
		Long l = number.longValue();
		return l.toString();
	}

}
